<?php
class Admin_Forms_UserForm extends Zend_Form {
	function __construct($option = null) {
		$this->setAction('')
				->setMethod('POST')
				->setAttrib('enctype', 'multipart/form-data')
				->setAttrib('class', 'form-horizontal')
                                ->setAttrib('style','float:left');
		
		
		$username = $this->createElement('text', 'username');
		$username->addValidator('alnum')
					->addValidator ( 'regex', false, array ('/^[a-z]+/' ) )
					->setRequired ( true )
		            ->addFilter('StringTrim')
		            ->setAttrib('class', 'input-xlarge')
		            ->setLabel('Tài khoản');
		$name = $this->createElement('text', 'fullName');
		$name->setRequired ( true )
		            ->addFilter('StringTrim')
		            ->setAttrib('class', 'input-xlarge')
		            ->setLabel('Tên');
        $password = $this->createElement ( 'password', 'password' );
		$password->addValidator ( 'StringLength', false, array (6,'messages' =>
                                      array(Zend_Validate_StringLength::TOO_SHORT => 'Mật khẩu không được phép ít hơn 6 ký tự')) )
        	     ->setRequired ( true )
        	     ->addValidator(
				        'NotEmpty',
				        true,
				        array('messages' => array('isEmpty' => 'Mật khẩu không được để trống'))
				    )
        	     ->setLabel('Mật khẩu')
        	     ->addFilter('StringTrim')
        	     ->setDescription('Tối thiểu là 6ký tự.
Chỉ sử dụng chữ và số');

		$mobile = $this->createElement('text', 'mobile');
		$mobile->addValidator(new Zend_Validate_Digits())
			->addFilter('StringTrim')
			->addValidator ( 'stringLength', false, array (10, 11,'messages' =>
                                      array(Zend_Validate_StringLength::TOO_LONG => 'Số điện thoại phải có 10 hoặc 11 số',Zend_Validate_StringLength::TOO_SHORT => 'Số điện thoại phải có 10 hoặc 11 số')))
            ->setLabel('Số điện thoại');
        $mail = $this->createElement('text', 'email');
		$mail->setRequired ( true )
					->addValidator('EmailAddress')
		            ->addFilter('StringTrim')
		            ->setAttrib('class', 'input-xlarge')
		            ->setLabel('Email');
		$birthday = $this->createElement('text', 'birthday');
//		$birthday->addValidator('date','dd-MM-YYYY')
		$birthday->addValidator(new Zend_Validate_Date('YYYY-MM-dd'),
									true,
									array('messages' =>
									array(Zend_Validate_Date::FALSEFORMAT => 'Ngày sinh phai theo định dạng Năm-tháng-ngày')))
					->setRequired()
					->addValidator(
				        'NotEmpty',
				        true,
				        array('messages' => array('isEmpty' => 'Sinh nhật không được để trống'))
				    )
				    ->setLabel('Sinh nhật');
		$gender = new Zend_Form_Element_Radio('sex');
        $gender->setLabel('Giới tính')
				->setMultiOptions(array('Nam', 'Nữ'))
				->setSeparator('')
				->setRequired(true)
				->setValue(0);

		$personal_id = $this->createElement('text', 'cmtnd');
		$personal_id->addValidator(new Zend_Validate_Digits())
			->addFilter('StringTrim')
			->addValidator ( 'stringLength', false, array (9, 9,'messages' =>
                                      array(Zend_Validate_StringLength::TOO_LONG => 'CMT phải có 9 chữ số',Zend_Validate_StringLength::TOO_SHORT => 'CMT phải có 9 chữ số')))
			->setRequired()
			->addValidator(
				        'NotEmpty',
				        true,
				        array('messages' => array('isEmpty' => 'Chứng minh thư không được để trống'))
				    )
            ->setLabel('Số chứng minh thư');
        $type = new Zend_Form_Element_Select('type', array(
					    'multiOptions' => array('0'	=> 'Yocity','1'	=> 'Yoyo')));
        $type->setLabel('Type');
        $cp_list = array();
        foreach ($option['cp_list'] as $value) {
        	$cp_list[$value['cpid']] = $value['cpName'];
        }
        $cp = new Zend_Form_Element_Select('cpid', array(
					    'multiOptions' => $cp_list));
        $cp->setLabel('CP');
		$submit = $this->createElement('submit','submit',array ('label' => $option['submit'] ));
		$submit->setAttrib('class', 'btn-large');
		$cancel = $this->createElement('button', 'cancel',array('label'	=> 'Hủy bỏ','class'=>'btn btn-large','onclick'	=> 'windown:location = "' . $option['cancel_link'] . '"'));
		$this->addElement($name)
				->addElement($username)
				->addElement($password)
				->addElement($mobile)
				->addElement($mail)
				->addElement($birthday)
				->addElement($gender)
				->addElement($personal_id)
				->addElement($type)
				->addElement($cp)
				->addElement($submit)
				->addElement($cancel);
		$this->setDecorators(array('FormElements',array('HtmlTag',array('tag'	=> 'div','width'=>'100%')),'form'));
		$this->setElementDecorators(array(
										'ViewHelper',
										'Errors',
										'Description',
										array(	
											array('data'	=> 'HtmlTag'),
											array('tag'		=> 'div','class'	=> 'controls')),
										array('label',
											array('class'	=> 'control-label','requiredSuffix'=>'(*)')),
										array(
											array('row'	=> 'HtmlTag',),
											array('class'=>'control-group ')
										),
									 	array(
								            array('ErrorsDiv' => 'HtmlTag'),
								            array('tag' => 'span', 'placement' => Zend_Form_Decorator_Abstract::APPEND)
		        						)
									)
								);
		$submit->setDecorators(array(
								'ViewHelper',
						       ));
		$cancel->setDecorators(array(
								'ViewHelper',
									)
						       );
		$this->addDisplayGroup(array('submit','cancel'),'action');
		$action = $this->getDisplayGroup('action');
		$action->setDecorators(array(
                'FormElements',
                'Fieldset',
                array('HtmlTag',array('tag'=>'div','class'	=> 'form-actions'))
        ));
	}
}